package org.example.controller;

import io.seata.core.context.RootContext;
import org.example.service.StorageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class StorageController {



    private static Logger logger = LoggerFactory.getLogger(StorageController.class) ;

    @Autowired
    private StorageService storageService ;

    /**
     * 扣减商品的库存
     * @param commodityCode 商品的编码
     * @param count 商品的数量
     * @return
     */
    @GetMapping("/deduct/{commodityCode}/{count}")
    public ResponseEntity<Void> deduct(@PathVariable("commodityCode") String commodityCode, @PathVariable("count") Integer count){
        logger.info("Account Service ... xid: " + RootContext.getXID());
        // 开始扣减库存
        storageService.deduct(commodityCode , count);
        return ResponseEntity.ok().build() ;
    }
}
